<?php


namespace backend\controllers;


use common\helpers\commonApi;
use common\models\Goods;
use common\models\GoodsClassify;
use Yii;

class ClassifyController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = GoodsClassify::className();
    }

    public function actionDrop()
    {
        $data = Yii::$app->request->post();
        $field = 'id';
        $id = intval($data['id']??0);
        if($id<1){
            return commonApi::message('参数错误', false);
        }
        $info = $this->model::findOne([$field=>$id]);
        if(!$info){
            return commonApi::message('信息不存在或已删除', false);
        }
        $goods = Goods::find()->where(['delete'=>0,'classify_id'=>$id])->count();
        $goods = $goods??0;
        if ($goods>0){
            return commonApi::message('该分类下存在'.$goods.'商品，禁止删除',false);
        }
        $result = $info->delete();
        if ($result) {
            return commonApi::message('删除成功', true, [], null, '');
        }
        return commonApi::message('删除失败', false);
    }
}